import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent } from '@delon/abc/table';
import { SFSchema, SFComponent, SFTreeSelectWidgetSchema } from '@delon/form';
import {HttpService,SupDic,GridComponent} from 'lbf'
import { NzMessageService} from 'ng-zorro-antd';
import { delay } from 'rxjs/operators';
import { of } from 'rxjs';

@Component({
  selector: 'app-dzda-clmu',
  templateUrl: './clmu.component.html',
  styleUrls: ['./clmu.component.less']
})
export class DzdaClmuComponent implements OnInit {

  param = {sname: 'dzda_clmucx',queryparas: {}}
  isAddVisible = false;
  isUpVisible = false;
  upFormData;

  isBqVisible = false;
  modalBqTitle:string;

  enum1: any;
  enum2: any;

  tags = [];
  inputVisible = false;
  inputValue = '';
  DAD028 :string;

  searchSchema: SFSchema = {
    properties: {
        DAD029: {
        type: 'string',
        title: '材料名称',
        ui: {
          grid: {span: 6}
        }
      },
      DAD080: {
        type: 'string',
        title: '渠道来源',
        enum: this.supdic.getSFDic('DAD080'),
        ui: {
          widget: 'select',
          allowClear: true,
          grid: {span: 6}
        }
      },
      DAD130: {
        type: 'string',
        title: '材料标签',
        enum: this.supdic.getSFDic('CLBQ'),
        ui: {
          widget: 'select',
          mode: 'tags',
          allowClear: true,
         grid: {span: 12}
        }
      }
    },
    ui: {
      spanLabelFixed: 100,
      grid:{span: 8}
    }
  };

  addSchema: SFSchema = {
    properties: {
      DAD029: {
        type: 'string',
        title: '材料名称'
      },
      DAD080: {
        type: 'string',
        title: '渠道来源',
        enum: this.supdic.getSFDic('DAD080'),
        ui: {
          widget: 'select',
          allowClear: true
        }
      },
      DAD081: {
        type: 'string',
        title: '来源渠道说明',
      },
      DAD082:{
        type: 'string',
        title: '是否实现共享',
        enum: this.supdic.getSFDic('YESNO'),
        ui: {
          widget: 'select',
          allowClear: true
        }
      }
    },
    required: ['DAD029','DAD080','DAD081','DAD082']
  }

  upSchema: SFSchema = {
    properties: {
      DAD029: {
        type: 'string',
        title: '材料名称'
      },
      DAD080: {
        type: 'string',
        title: '渠道来源',
        enum: this.supdic.getSFDic('DAD080'),
        ui: {
          widget: 'select',
          allowClear: true
        }
      },
      DAD081: {
        type: 'string',
        title: '来源渠道说明'
      },
      DAD082:{
        type: 'string',
        title: '是否实现共享',
        enum: this.supdic.getSFDic('YESNO'),
        ui: {
          widget: 'select',
          allowClear: true
        }
      }
    },
    required: ['DAD029','DAD080','DAD081','DAD082']
  }

  @ViewChild('sersf',{static: false}) sersf: SFComponent;
  @ViewChild('addSf',{static: false}) addSf: SFComponent;
  @ViewChild('upSf',{static: false}) upSf: SFComponent;
  @ViewChild('st', { static: false }) st: GridComponent;
  @ViewChild('inputElement', { static: false }) inputElement: ElementRef;
  
  columns: STColumn[] = [
    { title: '材料编码', index: 'DAD028'},// ,dic: 'DAH001'},
    { title: '材料名称', index: 'DAD029' },
    { title: '渠道来源',index: 'DAD080',dic: 'DAD080'},
    { title: '来源渠道说明',index: 'DAD081'},
    { title: '是否实现共享',index: 'DAD082',dic: 'YESNO'},
    {
      title: '操作区',
      buttons: [
          {
            text: '修改',
            click: (record) => this.update(record)
          },
          {
            text: '删除',
            click: (record) => this.del(record),
            pop: true,
            popTitle: '是否删除？'
          },
          {
            text: '标签维护',
            click: (record) => this.addClbq(record)
          }
      ]
    }
  ];

  constructor(private httpservice: HttpService,
              private supdic: SupDic,
              private msgSrv: NzMessageService
    ) { }

  ngOnInit() {
    
   }


  add() {
    this.addSf.refreshSchema();
    this.isAddVisible = true
  }

  handleCancel(){
    this.isAddVisible = false;
    this.isUpVisible = false;
    this.isBqVisible = false;
  }  

  saveAdd(data){
    this.isAddVisible = false;
    this.httpservice.lbservice('dzda_clmuxz',{para: this.addSf.value}).then(resdata => {
      if(resdata < 1){
        this.msgSrv.error(resdata.errmsg);
      }else{
        this.msgSrv.success('新增成功');
        this.st.reload();
      }
    })
  }

  query(){
    this.st.pi = 1
    this.st.reload(this.sersf.value);
    
  }

  update(record){
    console.log(record)
    this.isUpVisible = true;
    this.upFormData = record;
  }

  saveUp(){
    this.isUpVisible = false;
    this.httpservice.lbservice('dzda_clmuxg',{para: this.upSf.value}).then(resdata =>{
      if(resdata.code < 1){
        this.msgSrv.error(resdata.errmsg);
      }else{
        this.msgSrv.success('修改成功');
        this.st.st.reload();
      }
    })
  }

  del(record){
    this.httpservice.lbservice('dzda_clmusc',{DAD028: record.DAD028}).then(resdata =>{
      if(resdata.code < 1){
        this.msgSrv.error(resdata.errmsg);
      }else{
        this.msgSrv.success('删除成功');
        this.st.st.reload();
      }
    })
  }

  addClbq(rec){
    this.DAD028 = rec.DAD028;
    this.modalBqTitle = rec.DAD029+ ' 新增标签';
    this.httpservice.lbservice('dzda_getclbq',{DAD028: rec.DAD028}).then(res => {
      if(res.code >0 ){
        this.tags = res.message.list;
      }
    })

    setTimeout(() => {
      this.isBqVisible = true;
    }, 1000);

  }

  // tags
  handleClose(removedTag: {}): void {
    this.tags = this.tags.filter(tag => tag !== removedTag);
  }

  sliceTagName(tag: string): string {
    const isLongTag = tag.length > 20;
    return isLongTag ? `${tag.slice(0, 20)}...` : tag;
  }

  showInput(): void {
    this.inputVisible = true;
    setTimeout(() => {
      this.inputElement.nativeElement.focus();
    }, 10);
  }

  handleInputConfirm(): void {
    if (this.inputValue && this.tags.indexOf(this.inputValue) === -1) {
      this.tags = [...this.tags, this.inputValue];
    }
    this.inputValue = '';
    this.inputVisible = false;
  }

  savecl(){
    this.httpservice.lbservice('dzda_clbqxz',{DAD028: this.DAD028,TAGS: this.tags}).then(res => {
      if(res.code >0){
        this.msgSrv.success("材料标签更改成功")

        this.searchSchema.properties.DAD130.enum = res.message.dic;
        this.sersf.refreshSchema();
      }
    })
    this.isBqVisible = false;
  }

}
